package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import pojo.Customer;
import dao.CustomerDao;



@Repository(value = "customerDaoImpl")
public class CustomerDaoImpl implements CustomerDao{
	@Resource(name="dataSource1")
	private DataSource ds;
	

	public void setDs(DataSource ds) {
		this.ds = ds;
	}

	@Override
	public int addCustomer(Customer c) {
		String sql="insert into customer2(id,name,pwd)values(?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		ResultSet rs = null;
		int m = 0;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, c.getId());
			pst.setString(2, c.getName());
			pst.setString(3, c.getPwd());
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return m;
	}
	
	
	@Override
	public Customer findCustomerByName(String name) {
		String sql="select * from customer2 where name =?";
		Connection conn=null;
		PreparedStatement pst =null;
		ResultSet rs = null;
		int m=0;
		Customer c =null;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, name);
			rs=pst.executeQuery();
			while(rs.next()){
				c= new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setPwd(rs.getString("pwd"));
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return c;
	}

	@Override
	public Customer findCustomerByNameAndPwd(String name, String pwd) {
		String sql="select * from customer2 where name =? and pwd =?";
		Connection conn=null;
		PreparedStatement pst =null;
		ResultSet rs = null;
		int m=0;
		Customer c =null;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, pwd);
			rs=pst.executeQuery();
			while(rs.next()){
				c= new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setPwd(rs.getString("pwd"));
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return c;
	}

}
